<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot-scope="{row}" slot="menu">
        <el-button
          v-if="row.state === 4"
          type="text"
          size="small"
          @click.stop="handleToVerification(row)"
        >达达外送
        </el-button>
      </template>
      <template slot="expand" slot-scope="{row}">
        <div style="margin-left: 110px">
          <div>
            <span>收获地址：<el-tag type="success">{{ row.receivedAddress }}</el-tag></span>
            <span style="margin-left: 20px">联系人：<el-tag type="success">{{ row.receivedAddress }}(<a>{{ row.receivedTel }}</a>)</el-tag>
            </span>
          </div>
          <div>
          </div>
          <div :key="index" style="align-items: center; display: flex; margin-top: 5px" v-for="(item, index) in JSON.parse(row.goods)">
            <el-image style="width: 100px; height: 100px; border-radius: 5px" v-if="item.goods.pictureUrl" :src="item.goods.pictureUrl.split(',')[0]" />
            <span style="margin-left: 10px">  {{ item.goods.name }}（{{ item.num }}件）    </span>
            <el-tag style="margin-left: 10px" type="info">单价：￥{{ item.goods.price }}</el-tag>
            <el-tag style="margin-left: 10px" type="primary">小计：￥{{ item.goods.price * item.num }}</el-tag>
          </div>
        </div>
      </template>
      <template slot-scope="{row}"
                slot="state">
        <el-tag :key="i" v-for="(state, i) in stateDic" v-if="state.value === row.state" :type=state.type>{{ row.stateName }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="money">
        <el-tag type="primary">{{ row.money }}￥</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="refundMoney">
        <el-tag type="primary">￥{{ row.refundMoney }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="preferentialMoney">
        <el-tag type="primary">￥{{ row.preferentialMoney }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="originMoney">
        <el-tag type="primary">￥{{ row.originMoney }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="vipDiscount">
        <el-tag type="danger">￥{{ row.vipDiscount }}</el-tag>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, verification} from "@/api/order/order";
  import {mapGetters} from "vuex";
  import stateDic from "@/const/order/state";

  export default {
    data() {
      return {
        stateDic: stateDic,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          expand: true,
          expandRowKeys:[],
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: false,
          selection: true,
          column: [
            {
              label: "订单号",
              prop: "outTradeNo",
              search: true
            },
            {
              label: "下单人",
              prop: "memberName",
              slot: true
            },
            {
              label: "订单状态",
              prop: "state",
              rules: [{
                required: true,
                message: "请输入订单状态 1未付款,2已付款,3已接单,4配送中/待取货,5已收货,6已评价,7已取消,8.拒单，9申请退款,10已退款，11退款拒绝",
                trigger: "blur"
              }],
              slot: true
            },
            {
              label: "订单支付金额",
              prop: "money",
              rules: [{
                required: true,
                message: "请输入订单支付金额",
                trigger: "blur"
              }],
              slot: true
            },
            {
              label: "订单退款金额",
              prop: "refundMoney",
              hide: true,
              slot: true
            },
            {
              label: "满减金额",
              prop: "preferentialMoney",
              hide: true,
              slot: true
            },
            {
              label: "订单原始金额",
              prop: "originMoney",
              slot: true
            },
            {
              label: "会员折扣",
              prop: "vipDiscount",
              slot: true
            },
            {
              label: "付款时间",
              prop: "payTime",
              hide: true
            },
            {
              label: "订单时间",
              prop: "createdTime"
            },
            {
              label: "用户订单备注",
              prop: "userNote",
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: this.vaildData(this.permission.order_view, false),
          delBtn: false,
          editBtn: false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handleToVerification(row) {
        this.$confirm("核销后订单状态修改为完成, 确定核销订单?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return verification(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        this.query.orderType = 2
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
